
from rest_framework import serializers
from apps.head.models import Article
from apps.answer.models import Label, Question, Reply
from apps.users.models import User


class LabelSerializer(serializers.ModelSerializer):
    '''标签序列化器类'''
    class Meta:
        model=Label
        fields=('id','label_name')


class AnswerSerializer(serializers.ModelSerializer):
    '''等待,热门,最新'''
    labels=serializers.StringRelatedField(label="标签",many=True)
    class Meta:
        model=Question
        exclude="solve"


class IssueSerializer(serializers.ModelSerializer):
    '''发布问题'''
    labels=serializers.PrimaryKeyRelatedField(label="标签",read_only=True,many=True)
    class Meta:
        model=Question
        exclude="solve"


class UserSerializer(serializers.ModelSerializer):
    username=serializers.StringRelatedField(label="用户名")
    avatar=serializers.StringRelatedField()
    class Meta:
        model=User
        fields=("id","username","avatar")

class SubsSerializer(serializers.ModelSerializer):
    user=UserSerializer(label="用户",many=True)
    class Meta:
        model=Reply
        fields=("id","content","createtime","useful_count","unuseful_count","user")

class AnswerQuestionSerializer(serializers.ModelSerializer):
    '''问题回答'''
    user=UserSerializer(label="用户",many=True)
    subs=SubsSerializer(many=True,read_only=True)
    class Meta:
        model=Reply
        fields=('id',"content","createtime","useful_count","problem","unseful_count","subs","user","parent")

class CommentQuestionSerializer(serializers.ModelSerializer):
    subs=serializers.StringRelatedField(read_only=True,many=True)
    user=UserSerializer(label="用户",many=True)
    class Meta:
        model=Reply
        fields=("id","content","createtime","useful_count","problem","unuseful_count","subs","user","parent")

class IssueDatailsSerializer(serializers.ModelSerializer):
    '''问题详情'''
    labels=serializers.StringRelatedField(many=True,read_only=True)
    comment_question=CommentQuestionSerializer(label="问题评论列表",many=True)
    answer_question=AnswerQuestionSerializer(label="回答",many=True)
    class Meta:
        model=Question
        fields=("id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits","content","comment_question","answer_question")


class ReplySerializer(serializers.ModelSerializer):
    '''回答'''
    class Meta:
        model=Reply
        fields=("problem","content","type","parent")

class ArticleSerializers(serializers.ModelSerializer):
    """文章序列化器类"""
    collected_users = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    user = UserSerializer(read_only=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Question
        fields = ['id', 'title', 'content', 'createtime', 'user', 'collected', 'image', 'visits', 'collected_users']

class QuestionsSerializer(serializers.ModelSerializer):
    labels=serializers.StringRelatedField(many=True)
    class Meta:
        model=Article
        fields=("id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits")

class LablesSerializer(serializers.ModelSerializer):
    '''标签详情'''
    users=serializers.IntegerField()
    questions=[].append(QuestionsSerializer(many=True))

    class Meta:
        model=Label
        fields=("id","questions","label_name","desc","baike_url","label_icon","users")